package engine;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;
import util.Util;

public class Displays 
{

    private boolean fullscrn;
    public boolean fullscrn()
    {
        return fullscrn;
    }
    
    private boolean vsync;
    public boolean vsync()
    {
        return vsync;
    }
    
    //<editor-fold defaultstate="collapsed" desc="Screen Vars">
    private int screenW; // Specific to FS setting
    public int screenW() 
    {
        return screenW;
    }
    
    private int screenH;
    public int screenH() 
    {
        return screenH;
    }
    
    private int screenCX; // Specific to FS setting
    public int screenCX() 
    {
        return screenCX;
    }
    
    private int screenCY;
    public int screenCY() 
    {
        return screenCY;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Window Vars">
    private int windowW; // Specific to Windowed setting
    public int windowW() 
    {
        return windowW;
    }
    
    private int windowH;
    public int windowH() 
    {
        return windowH;
    }
    
    private int windowCX; // Specific to Windowed setting
    public int windowCX() 
    {
        return windowCX;
    }
    
    private int windowCY;
    public int windowCY() 
    {
        return windowCY;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Current Setting Vars">
    private int currentW; // Current: width, height
    public int currentW() 
    {
        return currentW;
    }
    
    private int currentH;
    public int currentH() 
    {
        return currentH;
    }
    
    private int currentCX; // Current: centerX, centerY
    public int currentCX() 
    {
        return currentCX;
    }
    
    private int currentCY;
    public int currentCY() 
    {
        return currentCY;
    }
    //</editor-fold>
    
    private int antialias;
    public int antialias()
    {
        return antialias;
    }
    
    private int fov;
    public int fov()
    {
        return fov;
    }
    
    private float aspectRatio;
    public float aspectRatio()
    {
        return aspectRatio;
    }
    
    public Displays(Prefs prefs) 
    {
        System.setProperty("org.lwjgl.opengl.Window.undecorated", "true");
        System.setProperty("org.lwjgl.util.NoChecks", "true");
        System.setProperty("org.lwjgl.util.Debug", "false");
        System.setProperty("org.lwjgl.opengl.Display.nomouse", "true");
        System.setProperty("org.lwjgl.opengl.Display.nokeyboard", "true");
        System.setProperty("org.lwjgl.opengl.Display.noinput", "true");
        fullscrn = prefs.fullscrn();
        vsync = prefs.vsync();
        fov = prefs.fov();
        windowW = prefs.windowW();
        windowH = prefs.windowH();
        antialias = prefs.antialias();
        screenW = Util.getScreenW();
        screenH = Util.getScreenH();
        windowCX = windowW / 2;
        windowCY = windowH / 2;
        screenCX = screenW / 2;
        screenCY = screenH / 2;
        if (fullscrn)
        {
            currentW = screenW;
            currentH = screenH;
            currentCX = screenCX;
            currentCY = screenCY;
        }
        else
        {
            currentW = windowW;
            currentH = windowH;
            currentCX = windowCX;
            currentCY = windowCY;
        }
        aspectRatio = ((float) currentW) / currentH;
        try 
        {
            Display.setFullscreen(fullscrn);
            if (!fullscrn)
            {
                Display.setDisplayMode(new DisplayMode(windowW, windowH));
            }
            Display.setVSyncEnabled(vsync);
            Display.setTitle("JCOGLGE");
            if (antialias == 0)
            {
                Display.create();
            }
            else
            {
                Display.create(new PixelFormat(8, 0, 0, antialias));
            }
        }
        catch (Exception e)
        {
            Util.console(e);
            Util.quit();
        }
    }

    public boolean isCloseRequested() 
    {
        return Display.isCloseRequested();
    }

    public void updateScreen() 
    {
        Display.update();
    }

    public void destroyDisplay() 
    {
        Display.destroy();
    }

}
